<?php
include_once 'checkAdmin.php';
?>
<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>后台管理系统</title>
    <style>
        .main {
            width: 80%;
            margin: 0;
            text-align: center
        }

        h2 {
            font-size: 16px
        }

        h2 a {
            color: navy;
            text-decoration;
            none;
            margin-right: 15px
        }

        h2 a:last-child {
            margin-right: 0
        }

        h2 a:hover {
            color: brown;
            text-decoration: underline
        }

        /*悬停hover效果 */
        tr:hover {
            background-color: aqua
        }

        .trClick1 {
            background-color: yellow
        }

        .trClick2 {
            background-color: white
        }
    </style>
</head>

<body>
    <div class="main">
        <?php
        include_once "nav.php";
        include_once "connect.php";
        include_once "page.php";
        $sql = 'select count(id) as total from info';    //聚合函数
        $result = mysqli_query($conn, $sql);
        $info = mysqli_fetch_array($result);
        $total = $info['total'];    //记录总数
        $perPage = 8;   //设置每页显示的个数
        $page = $_GET['page'] ?? 1; //读取当前页码
        paging($total, $perPage);   //引用分页函数
        $sql = "select * from info order by id desc limit $firstCount,$displayPG";
        $result = mysqli_query($conn, $sql);
        ?>
        <table border="1" cellspacing="0" cellpadding="10" style="border-collapse: collapse" align="center" width="90%">
            <tr>
                <td>序号</td>
                <td>用户名</td>
                <td>性别</td>
                <td>邮箱</td>
                <td>爱好</td>
                <td>是否为管理员</td>
                <td>操作</td>
            </tr>
            <?php
            //行号
            $i = 1;
            while ($info = mysqli_fetch_array($result)) {
            ?>
                <tr onclick="if(this.className == 'trClick1') {this.className = 'trClick2'} else {this.className = 'trClick1'}">
                    <td><?php echo $firstCount + $i; ?></td>
                    <td><?php echo $info['username']; ?></td>
                    <td><?php echo $info['sex'] ? '男' : '女'; ?></td>
                    <td><?php echo $info['email']; ?></td>
                    <td><?php echo $info['fav']; ?></td>
                    <td><?php echo $info['admin'] ? '✔' : ''; ?></td> <!-- ‘√’ 显示为管理员--!>
                    <td>
                        <?php if ($info['username'] != 'admin') { ?>
                            <a href="modify.php?id=4&username=<?php echo $info['username']; ?>&source=admin&page=<?php echo $page; ?>">修改资料 / </a>
                            <a href="del.php?id=<?php echo $info['id']; ?>&username=<?php echo $info['username']; ?>">删除会员 / </a>
                            <?php
                            if (!$info['admin']) {
                            ?><a href="setAdmin.php?action=1&id=<?php echo $info['id']; ?>" >设为管理员</a>
                                <?php
                            } else {
                                ?><a href="setAdmin.php?action=0&id=<?php echo $info['id']; ?>" >取消管理员</a>
                            <?php } ?>
                        <?php } else { ?>
                            <span style="color:gray"> 此账户为超级管理 </span>
                        <?php } ?>
                    </td>
                </tr>
            <?php
                $i++;
            }
            ?>
        </table>
        <?php
        echo $pageNav;
        ?>
    </div>
<script>
    function del(id, name) {
        <?php echo "执行js"; ?>
        if(confirm('你确定删除会员 ' + name + ?)) {
            location.href = 'del.php?id=' + id + '&username=' + name;
        }
    }
</script>
</body>
</html>